/*
 * CArbre.cpp
 *
 *      Author: grifos
 */

#include "CArbre.h"

CArbre::CArbre() {
	_root = new osg::MatrixTransform;
	_root->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(0.0f),1.0f,0.0f,0.0f));
	osg::ref_ptr<osg::Switch> s = new osg::Switch;
	osg::ref_ptr<osg::Geode> geode1 = new osg::Geode;
	osg::ref_ptr<osg::Geode> geode2 = new osg::Geode;
	osg::Texture2D *arbreTexture = new osg::Texture2D;
	arbreTexture->setImage(osgDB::readImageFile("./ressource/arbre.png"));

	osg::AlphaFunc* alphaFunc = new osg::AlphaFunc;
	alphaFunc->setFunction(osg::AlphaFunc::GEQUAL,0.05f);

	osg::StateSet* arbreSet = new osg::StateSet;

	arbreSet->setMode( GL_LIGHTING, osg::StateAttribute::ON );
	arbreSet->setTextureAttributeAndModes
		  (0, arbreTexture, osg::StateAttribute::ON );
	arbreSet->setAttributeAndModes
		  (new osg::BlendFunc, osg::StateAttribute::ON );
	osg::AlphaFunc* alphaFunction = new osg::AlphaFunc;
	alphaFunction->setFunction(osg::AlphaFunc::GEQUAL,0.00f);
	arbreSet->setAttributeAndModes( alphaFunc, osg::StateAttribute::ON );

	geode1->addDrawable(createDrawable(0,15.0f,arbreSet));
	geode2->addDrawable(createDrawable(1,15.0f,arbreSet));
	s->addChild(geode2.get(),true);
	s->addChild(geode1.get(),true);
	_root->addChild(s.get());
}

CArbre::~CArbre() {
	// TODO Auto-generated destructor stub
}

osg::Drawable * CArbre::createDrawable(int mode, const float & scale, osg::StateSet* bbState)
{
	float width = 1.0f;
	float height = 2.0f;

	width *= scale;
	height *= scale;

	osg::Geometry* shrubQuad = new osg::Geometry;

	osg::Vec3Array* shrubVerts = new osg::Vec3Array(4);
	if(mode==0)
	{
		(*shrubVerts)[0] = osg::Vec3(-width/2.0f, 0, 0);
		(*shrubVerts)[1] = osg::Vec3( width/2.0f, 0, 0);
		(*shrubVerts)[2] = osg::Vec3( width/2.0f, 0, height);
		(*shrubVerts)[3] = osg::Vec3(-width/2.0f, 0, height);
	}
	else
	{
		(*shrubVerts)[0] = osg::Vec3(0, -width/2.0f, 0);
		(*shrubVerts)[1] = osg::Vec3( 0, width/2.0f, 0);
		(*shrubVerts)[2] = osg::Vec3( 0, width/2.0f, height);
		(*shrubVerts)[3] = osg::Vec3(0, -width/2.0f, height);
	}

	shrubQuad->setVertexArray(shrubVerts);

	osg::Vec2Array* shrubTexCoords = new osg::Vec2Array(4);
	(*shrubTexCoords)[0].set(0.0f,0.0f);
	(*shrubTexCoords)[1].set(1.0f,0.0f);
	(*shrubTexCoords)[2].set(1.0f,1.0f);
	(*shrubTexCoords)[3].set(0.0f,1.0f);
	shrubQuad->setTexCoordArray(0,shrubTexCoords);

	shrubQuad->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));

	// Need to assign a color to the underlying geometry, otherwise we'll get
	// whatever color is current applied to our geometry.
	// Create a color array, add a single color to use for all the vertices

	osg::Vec4Array* colorArray = new osg::Vec4Array;
	colorArray->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) ); // white, fully opaque

	// An index array for assigning vertices to colors (based on index in the array)
	osg::TemplateIndexArray
	  <unsigned int, osg::Array::UIntArrayType,4,1> *colorIndexArray;
	colorIndexArray =
	  new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,1>;
	colorIndexArray->push_back(0);

	// Use the index array to associate the first entry in our index array with all
	// of the vertices.
	shrubQuad->setColorArray( colorArray);
	shrubQuad->setColorIndices(colorIndexArray);
	shrubQuad->setColorBinding(osg::Geometry::BIND_OVERALL);

	shrubQuad->setStateSet(bbState);

	return shrubQuad;

}
